//
//  LoginViewController.m
//  Ev_Master
//
//  Created by Evan on 2019/6/13.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import <Masonry.h>
#import "UserCenter.h"
#import "ForgetPwdViewController.h"
#import "LoginSuccessViewController.h"
#import "HomeViewController.h"
#import "taskUserService.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIView *viewLogin;

@property (weak, nonatomic) IBOutlet UITextField *txtPhone;
@property (weak, nonatomic) IBOutlet UITextField *txtPwd;

@property(nonatomic, copy) LoginView   *viewLo;

@end

@implementation LoginViewController{
    bool isLogin;
}

- (void)viewDidLoad {
//    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setUpUI];
    
    [[UserCenter shareCenter] clearUserState];
    
//    viewRegister.blockRegister = ^(void) {
////        LoginViewController *vc =[[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
//        LoginSuccessViewController *vc = [[LoginSuccessViewController alloc]initWithNibName:@"LoginSuccessViewController" bundle:nil];
//        vc.labelTitle.text = @"恭喜你，注册成功";
//        [weakSelf.navigationController pushViewController:vc animated:YES];
//    };
}

-(void)viewWillAppear:(BOOL)animated{
    [self setHiddenNavigation:YES];
    [self hideHeadView:YES];
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}

- (IBAction)btnLogin:(id)sender {
    if (self.txtPhone.text.length < 1) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的登录账号"];
        return;
    }
    if (self.txtPwd.text.length < 1) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的登录账号"];
        return;
    }
    
    [DKProgressHUD showLoadingWithStatus:@"加载中..."];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.txtPhone.text forKey:@"username"];
    [dict setValue:self.txtPwd.text forKey:@"password"];
    [dict setValue:@"app" forKey:@"group"];
    
    [taskUserService getLogin:dict success:^(modelLoginData * _Nonnull response, BOOL success) {
        NSLog(@"登录成功");
        [DKProgressHUD dismiss];
        [[NetworkManager sharedManager].requestSerializer setValue:response.data.access_token forHTTPHeaderField:@"access_token"];
        [[NSUserDefaults standardUserDefaults] setObject:response.data.access_token forKey:SAVE_KEY_AUTH];
        [UserCenter shareCenter].authToken = response.data.access_token;
        [UserCenter shareCenter].userNo =  response.data.username;
        
        [[UserCenter shareCenter] getProfiles:^(modelUserInfo *profile) {
            [[NSNotificationCenter defaultCenter] postNotificationName:Notification_login_ok_notice object:nil userInfo:nil];
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                NSLog(@"c登录成功并隐藏登录框！");
            }];
        }];
        
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        NSLog(@"登录失败：%@",strDescription);
        [DKProgressHUD dismiss];
        if (error.domain == NSURLErrorDomain) {
            [DKProgressHUD showErrorWithStatus:@"网络错误"];
            return;
        }
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (IBAction)btnForgetPwd:(id)sender {
    ForgetPwdViewController *vc = [[ForgetPwdViewController alloc]initWithNibName:@"ForgetPwdViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)btnRegister:(id)sender {
    RegisterViewController *vc = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}




- (void)setUpUI{
    
    CAGradientLayer *gl2 = [CAGradientLayer layer];
    gl2.frame = CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);
    gl2.startPoint = CGPointMake(0, 0);
    gl2.endPoint = CGPointMake(1, 1);
    gl2.colors = @[(__bridge id)[UIColor colorWithRed:51/255.0 green:224/255.0 blue:240/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:72/255.0 green:146/255.0 blue:236/255.0 alpha:1.0].CGColor];
    gl2.locations = @[@(0.0),@(1.0)];
    
    [self.view.layer insertSublayer:gl2 atIndex:0];
    
    self.txtPhone.attributedPlaceholder = [self attrStr:self.txtPhone strPlaceholder:@"请输入手机号"];
    self.txtPwd.attributedPlaceholder = [self attrStr:self.txtPwd strPlaceholder:@"请输入密码"];
}


- (NSAttributedString*) attrStr:(UITextField*)txtField strPlaceholder:(NSString*)str{
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:str attributes: @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:txtField.font}];
    return attrString;
}

@end
